package com.groupfour.attendancemanagementsystem.service.impl;
/**
 * @version: V1.0
 * @author: 章珑赢
 * @className: GroupRatingServiceImpl
 * @packageName: service.impl
 * @description: 小组评分
 * @data: 2022/4/23
 */
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.groupfour.attendancemanagementsystem.dao.GroupRatingDao;
import com.groupfour.attendancemanagementsystem.entity.DTO.GroupDetailDTO;
import com.groupfour.attendancemanagementsystem.entity.DTO.GroupRatingDTO;
import com.groupfour.attendancemanagementsystem.entity.VO.DepartmentAndGroupVO;
import com.groupfour.attendancemanagementsystem.entity.VO.GroupDetailVO;
import com.groupfour.attendancemanagementsystem.entity.VO.GroupRatingVO;
import com.groupfour.attendancemanagementsystem.entity.VO.HomeWorkDetailsVO;
import com.groupfour.attendancemanagementsystem.service.GroupRatingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class GroupRatingServiceImpl implements GroupRatingService {
    @Autowired
    private GroupRatingDao groupRatingDao;

    /**
     * @version: V1.0
     * @author: 章珑赢
     * @methodsName: SelectAllGroupRating
     * @description: 查询所有小组评分的接口实现类
     * @param:  无
     * @return: GroupRatingVO集合
     * @throws: 无
     **/
    public Map<String, Object> SelectAllGroupRating(Integer pageNum, Integer pageSize, String userCode) {
        Map<String,Object> map = new HashMap<>();
//        查询结果
        List<GroupRatingDTO> GroupRatingDTO = groupRatingDao.SelectAllGroupRating(userCode);

        List<GroupRatingVO> groupRatingsVOList = new ArrayList<>();

        GroupRatingVO groupRatingVO=new GroupRatingVO();
        for (GroupRatingDTO groupRatingDTO : GroupRatingDTO) {
            groupRatingVO.setMembership(groupRatingDao.groupAccount(groupRatingDTO.getGroupCode()));
            groupRatingVO.setGroupName(groupRatingDTO.getGroupName());
            groupRatingVO.setGroupLeader(groupRatingDTO.getGroupLeader());
            groupRatingVO.setCourseName(groupRatingDTO.getCourseName());
            groupRatingVO.setDepartmentName(groupRatingDTO.getDepartmentName());
            groupRatingVO.setDutyTeacher(groupRatingDTO.getDutyTeacher());
            groupRatingVO.setGroupSyntnesisScore(groupRatingDTO.getGroupSyntnesisScore());
            groupRatingVO.setGroupCode(groupRatingDTO.getGroupCode());
            groupRatingsVOList.add(groupRatingVO);
        }
        //清理pageHelper缓存
        PageHelper.clearPage();
        // 创建分页对象
        PageHelper.startPage(pageNum,pageSize);
        PageInfo<GroupRatingVO> groupRatingVOPageInfo = new PageInfo<>(groupRatingsVOList);

        map.put("pageCount",groupRatingVOPageInfo.getTotal());
        map.put("result",groupRatingVOPageInfo.getList());

        return map;
    }

    /**
     * @version: V1.0
     * @author: 章珑赢
     * @methodsName: SelectByProjectNameOrGroupName
     * @description: 根据项目名称或小组名称查询所有小组评分的接口实现类
     * @param:  projectName(项目名称)，groupName(小组评分)
     * @return: GroupRatingVO集合
     * @throws: 无
     **/
    public Map<String,Object> SelectGroupRating(String projectName,String groupName,Integer pageNum,Integer pageSize,String userCode) {
        Map<String,Object> map = new HashMap<>();
        // 查询结果
        List<GroupRatingDTO> GroupRatingDTO=groupRatingDao.selectGroupRating(projectName,groupName,userCode);
        List<GroupRatingVO> groupRatingsVOList=new ArrayList<>();
        GroupRatingVO groupRatingVO=new GroupRatingVO();
        //        将GroupRatingDTO集合的值赋予给GroupRatingVO
        for (GroupRatingDTO groupRatingDTO : GroupRatingDTO) {
            groupRatingVO.setMembership(groupRatingDao.groupAccount(groupRatingDTO.getGroupCode()));
            groupRatingVO.setGroupName(groupRatingDTO.getGroupName());
            groupRatingVO.setGroupLeader(groupRatingDTO.getGroupLeader());
            groupRatingVO.setCourseName(groupRatingDTO.getCourseName());
            groupRatingVO.setDepartmentName(groupRatingDTO.getDepartmentName());
            groupRatingVO.setDutyTeacher(groupRatingDTO.getDutyTeacher());
            groupRatingVO.setGroupSyntnesisScore(groupRatingDTO.getGroupSyntnesisScore());
            groupRatingVO.setGroupCode(groupRatingDTO.getGroupCode());
            groupRatingsVOList.add(groupRatingVO);
        }
        // 清理pageHelper缓存
        PageHelper.clearPage();
        // 创建分页对象
        PageHelper.startPage(pageNum,pageSize);
        PageInfo<GroupRatingVO> groupRatingVOPageInfo = new PageInfo<>(groupRatingsVOList);

        map.put("pageCount",groupRatingVOPageInfo.getTotal());
        map.put("result",groupRatingVOPageInfo.getList());
        return map;
    }

    /**
     * @version: V1.0
     * @author: 章珑赢
     * @methodsName: SelectGroupDetails
     * @description: 查询小组的详情
     * @param:  groupCode（小组编号）
     * @return: GroupDetailVO实体类
     * @throws: 无
     **/
    @Override
    public GroupDetailVO SelectGroupDetails(String groupCode) {
        List<GroupDetailDTO>trainTeacher=groupRatingDao.SelectGroupTrainTeacher(groupCode);
        List<GroupDetailDTO>students=groupRatingDao.SelectGroupStudent(groupCode);
        GroupDetailVO groupDetailVO = groupRatingDao.SelectGroupDetails(groupCode);
        groupDetailVO.setMembers(students);
        groupDetailVO.setTrainTeacher(trainTeacher);
        return groupDetailVO;
    }

    /**
     * @version: V1.0
     * @author: 章珑赢
     * @methodsName: SelectHomeWorks
     * @description: 查询小组的作业信息
     * @param: groupCode（小组编号）
     * @return: HomeWorkDetailsVO集合
     * @throws: 无
     **/
    @Override
    public Map<String, Object> SelectHomeWorks(String groupCode, String studentCode, Integer pageNum, Integer pageSize) {
        Map<String,Object> map = new HashMap<>();
        // 清理pageHelper缓存
        PageHelper.clearPage();
        // 创建分页对象
        PageHelper.startPage(pageNum, pageSize);
        // 查询结果
        PageInfo<HomeWorkDetailsVO> homeWorkDetailsVOPageInfo = new PageInfo<>(groupRatingDao.SelectHomeWorks(groupCode, studentCode));

        map.put("pageCount",homeWorkDetailsVOPageInfo.getTotal());
        map.put("result",homeWorkDetailsVOPageInfo.getList());
        return map;
    }
    /**
     * @version: V1.0
     * @author: 章珑赢
     * @methodsName: DoPersonalGradeAndComment
     * @description: 进行小组打分和评论
     * @param:  groupCode（小组编号），groupSyntnesisScore（小组分数），groupComment（小组评论）
     * @return: boolean
     * @throws: 无
     **/
    @Override
    public boolean DoGroupGradeAndComment(String groupSyntnesisScore, String groupComment, String groupCode) {
        return groupRatingDao.DoGroupGradeAndComment(groupSyntnesisScore,groupComment,groupCode);
    }
    /**
     * @version: V1.0
     * @author: 章珑赢
     * @methodsName: selectDepartmentAndGroup
     * @description: 进行小组打分和评论
     * @param:  无
     * @return: Result
     * @throws: 无
     **/
    @Override
    public DepartmentAndGroupVO selectCourseAndGroup() {
        List<GroupRatingVO> groupRatingVOList=groupRatingDao.selectCourseAndGroup();
        DepartmentAndGroupVO departmentAndGroupVO=new DepartmentAndGroupVO();
        List groupList = new ArrayList();
        List courseList=new ArrayList();
        for (GroupRatingVO groupRatingVO : groupRatingVOList) {
            courseList.add(groupRatingVO.getCourseName()) ;
           groupList.add(groupRatingVO.getGroupName());
        }
        departmentAndGroupVO.setCourse((List) courseList.stream().distinct().collect(Collectors.toList()));
        departmentAndGroupVO.setGroup((List) groupList.stream().distinct().collect(Collectors.toList()));
        return departmentAndGroupVO ;
    }
}
